package com.winderinfo.yikaotianxia.ui.my;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseFragment;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.coupons.MyCouponsActivity;
import com.winderinfo.yikaotianxia.ui.order.OrderActivity;
import com.winderinfo.yikaotianxia.ui.setup.FeedbackList;
import com.winderinfo.yikaotianxia.ui.setup.SetUpActivity;
import com.winderinfo.yikaotianxia.ui.shoppingcart.ShoppingCartActivity;
import com.winderinfo.yikaotianxia.ui.studycards.StudyCardsActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class MyFragment extends BaseFragment implements Contract.View {
    @BindView(R.id.my_carnum)
    TextView myCarnum;
    @BindView(R.id.my_image)
    ImageView myImage;
    @BindView(R.id.my_ordernum)
    TextView myOrdernum;
    @BindView(R.id.my_coursenum)
    TextView myCoursenum;
    @BindView(R.id.my_couponsnum)
    TextView myCouponsnum;
    @BindView(R.id.my_learsenum)
    TextView myLearsenum;
    @BindView(R.id.my_name)
    TextView myName;
    @BindView(R.id.my_order)
    LinearLayout myOrder;
    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();
    private SPUtils spUtils;

    @Override
    protected int initLayoutId() {
        return R.layout.fragment_me;
    }

    @Override
    protected void initView(View view) {
        //实例P
        presenter = new Presenter(this);
        spUtils = new SPUtils(getActivity());
    }

    @Override
    protected void initData() {
        //获取用户Id
        String userID = spUtils.getString("UserID", " ");
        if (userID != null) {
            /**
             * @id 用户Id
             * */
            map.clear();
            map.put("id", 1);
            presenter.onPostRequest(Api.USERXIANGQIN, map, UserBean.class);
        } else {
            ToastUtil.showSuccess(getActivity(), "请先登录");
            myCarnum.setVisibility(View.GONE);
        }
    }

    @Override
    public void onSuccess(Object data) {
        //用户详情
        if (data instanceof UserBean) {
            //存手机号
            spUtils.putString("Phone", ((UserBean) data).getYkUser().getPhone());
            //设置值
            Glide.with(this)
                    .load(((UserBean) data).getYkUser().getPhoto())
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(myImage);
            myCarnum.setText(((UserBean) data).getYkUser().getCartnum() + "");
            myName.setText(((UserBean) data).getYkUser().getUsername());
            myLearsenum.setText(((UserBean) data).getYkUser().getLearnnum() + "");
            myCouponsnum.setText(((UserBean) data).getYkUser().getCouponsnum() + "");
            myOrdernum.setText(((UserBean) data).getYkUser().getOrdernum() + "");
            myCoursenum.setText(((UserBean) data).getYkUser().getCoursenum() + "");
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }

    @OnClick({R.id.my_order, R.id.my_course, R.id.my_coupon, R.id.my_cards, R.id.my_set, R.id.my_feedback, R.id.my_shopping})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.my_order:
                openActivity(OrderActivity.class);
                break;
            case R.id.my_course:
                break;
            case R.id.my_coupon:
                openActivity(MyCouponsActivity.class);
                break;
            case R.id.my_cards:
                openActivity(StudyCardsActivity.class);
                break;
            case R.id.my_set:
                openActivity(SetUpActivity.class);
                break;
            case R.id.my_feedback:
                openActivity(FeedbackList.class);
                break;
            case R.id.my_shopping:
                openActivity(ShoppingCartActivity.class);
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
